package com.etc.common;

import java.text.SimpleDateFormat;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.github.pagehelper.PageInfo;

/**
 * 
 * Filename:    CodeMessage.java  
 * Description: ajax json统一出口   
 * Copyright:   Copyright (c) 2018-2019 All Rights Reserved.
 * Company:     chinasofti.com Inc.
 * @author:     mazheng 
 * @version:    1.0  
 * Create at:   2020年3月2日 上午8:16:20  
 *  
 * Modification History:  
 * Date         Author      Version     Description  
 * ------------------------------------------------------------------  
 * 2020年3月2日      mazheng      1.0         1.0 Version  
 *
 */
public class CodeMessage {

    private Code code;
    private String msg;
    private Object data;
   
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
    public Code getCode() {
        return code;
    }
    public void setCode(Code code) {
        this.code = code;
    }
    public static CodeMessage faild(Code filederror, String str) {
        CodeMessage cm = new CodeMessage();
        cm.setCode(filederror);
        cm.setMsg(str);
        return cm;
    }
    public static CodeMessage success(Code filederror, String msg, Object data) {
        CodeMessage cm = new CodeMessage();
        cm.setCode(filederror);
        cm.setMsg(msg);
        cm.setData(data);
        return cm;
    }
    @Override
    public String toString() {
        return "CodeMessage [code=" + code + ", msg=" + msg + ", data=" + data + "]";
    }
    public static String success(Code sucess, String msg, Object p, boolean b) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);  
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
        String data = "";
        try {
            data = objectMapper.writeValueAsString(CodeMessage.success(sucess, msg, p));
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            CodeMessage.faild(Code.FAIL, "日期类型转换失败");
        }
        return data;
    }
    
}
